package com.itheima.question.timer;

import com.itheima.common.contants.RedisPicConstants;
import com.itheima.common.safe.ImgSafe;
import com.itheima.common.safe.TextSafe;
import com.itheima.question.config.FastDFSClientUtil;
import com.itheima.question.mapper.QuestionMapper;
import com.itheima.question.pojo.Question;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Slf4j
public class AuditTask {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;

    @Autowired
    private QuestionMapper questionMapper;
    @Scheduled(cron = "0/5 12 * 1 * ?")
    private void autoAuditText(){
        TextSafe textSafe = new TextSafe();
        Set<String> audit_all = redisTemplate.opsForSet().members(RedisPicConstants.AUDIT_ALL);
        log.info("定时任务开始执行，需要审核的内容有"+audit_all);
        for (String i:audit_all)
        {
            Question question = questionMapper.selectById(i);
            try {
                String suggesstion = textSafe.TextSafe(question.getSubject());
                if (suggesstion.equals("pass")){
                    question.setReviewStatus("1");
                    questionMapper.updateById(question);
                    redisTemplate.opsForSet().remove(RedisPicConstants.AUDIT_ALL,i);
                }
                else{
                    question.setReviewStatus("-1");
                    questionMapper.updateById(question);
                    redisTemplate.opsForSet().remove(RedisPicConstants.AUDIT_ALL,i);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Scheduled(cron = "0/5 * * * * ?")
    private void autoAuditImg(){
        ImgSafe imgSafe = new ImgSafe();
        Set<String> audit_all = redisTemplate.opsForSet().members(RedisPicConstants.AUDIT_ALL);
        log.info("定时任务开始执行，需要审核的图片有"+audit_all);
        for (String i:audit_all)
        {
            Question question = questionMapper.selectById(i);
            try {
                String suggesstion = imgSafe.ImgSafe(question.getPicture());
                if (suggesstion.equals("pass")){
                    question.setReviewStatus("1");
                    questionMapper.updateById(question);
                    redisTemplate.opsForSet().remove(RedisPicConstants.AUDIT_ALL,i);
                }
                else{
                    question.setReviewStatus("-1");
                    questionMapper.updateById(question);
                    redisTemplate.opsForSet().remove(RedisPicConstants.AUDIT_ALL,i);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
